package net.oschina.android.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.oschina.android.app.AppException;
import net.oschina.android.util.StringUtil;
import net.oschina.android.util.VolleyRequestQueue;
import net.oschina.android.util.XmlRequest;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;

/**
 * 新闻列表实体类
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class NewsList extends Entity{
	
	static NewsList newslistInstance = new NewsList();
	static News news = null;

	public final static int CATALOG_ALL = 1;
	public final static int CATALOG_INTEGRATION = 2;
	public final static int CATALOG_SOFTWARE = 3;
	
	private int catalog;
	private int pageSize;
	private int newsCount;
	private List<News> newslist = new ArrayList<News>();
	
	public int getCatalog() {
		return catalog;
	}
	public int getPageSize() {
		return pageSize;
	}
	public int getNewsCount() {
		return newsCount;
	}
	public List<News> getNewslist() {
		return newslist;
	}
	
	public static NewsList parse(Context context,String url) throws IOException, AppException {
		
		XmlRequest xmlRequest = new XmlRequest(context,url , new Response.Listener<XmlPullParser>() {

			@Override
			public void onResponse(XmlPullParser response) {
				
				//获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
				try {
		            int evtType=response.getEventType();
					//一直循环，直到文档结束
					while(evtType!=XmlPullParser.END_DOCUMENT){
			    		String tag = response.getName(); 
					    switch(evtType){ 
					    	case XmlPullParser.START_TAG:
					    		if(tag.equalsIgnoreCase("catalog")) {
					    			newslistInstance.catalog = StringUtil.toInt(response.nextText(),0);
					    		}
					    		else if(tag.equalsIgnoreCase("pageSize")) {
					    			newslistInstance.pageSize = StringUtil.toInt(response.nextText(),0);
					    		}
					    		else if(tag.equalsIgnoreCase("newsCount")) {
					    			newslistInstance.newsCount = StringUtil.toInt(response.nextText(),0);
					    		}
					    		else if (tag.equalsIgnoreCase(News.NODE_START)) {
					    			news = new News();
					    		}
					    		else if(news != null){
						            if(tag.equalsIgnoreCase(News.NODE_ID)){
						            	news.id = StringUtil.toInt(response.nextText(),0);
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_TITLE)){
						            	news.setTitle(response.nextText());
						            	Log.e("解析资讯的标题为", news.getTitle().toString());
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_URL)){			            	
						            	news.setUrl(response.nextText());
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_AUTHOR)){			            	
						            	news.setAuthor(response.nextText());		            	
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_AUTHORID)){			            	
						            	news.setAuthorId(StringUtil.toInt(response.nextText(),0));		            	
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_COMMENTCOUNT)){			            	
						            	news.setCommentCount(StringUtil.toInt(response.nextText(),0));			            	
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_PUBDATE)){			            	
						            	news.setPubDate(response.nextText());	
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_TYPE)){
						            	news.getNewType().type = StringUtil.toInt(response.nextText(),0); 
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_ATTACHMENT)){
						            	news.getNewType().attachment = response.nextText(); 	
						            }
						            else if(tag.equalsIgnoreCase(News.NODE_AUTHORUID2)){
						            	news.getNewType().authoruid2 = StringUtil.toInt(response.nextText(),0); 
						            }
					    		}
					            //通知信息
					    		else if(tag.equalsIgnoreCase("notice")){
					            	newslistInstance.setNotice(new Notice());
					    		}else if(newslistInstance.getNotice() != null){
					    			if(tag.equalsIgnoreCase("atmeCount")){			      
					    				newslistInstance.getNotice().setAtmeCount(StringUtil.toInt(response.nextText(),0));
						            }else if(tag.equalsIgnoreCase("msgCount")){			            	
						            	newslistInstance.getNotice().setMsgCount(StringUtil.toInt(response.nextText(),0));
						            }else if(tag.equalsIgnoreCase("reviewCount")){			            	
						            	newslistInstance.getNotice().setReviewCount(StringUtil.toInt(response.nextText(),0));
						            }else if(tag.equalsIgnoreCase("newFansCount")){			            	
						            	newslistInstance.getNotice().setNewFansCount(StringUtil.toInt(response.nextText(),0));
						            }
					    		}
					    		break;
					    	case XmlPullParser.END_TAG:	
							   	//如果遇到标签结束，则把对象添加进集合中
						       	if (tag.equalsIgnoreCase("news") && news != null) { 
						       		newslistInstance.getNewslist().add(news); 
						           news = null; 
						       	}
						       	break; 
					    }
					    //如果xml没有结束，则导航到下一个节点
					    evtType=response.next();
					}	
		        }catch (XmlPullParserException e) {
		        	e.printStackTrace();
		        } catch (IOException e) {
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError arg0) {
				Log.e("解析资讯列表失败：", "YES");
			}
		});
		VolleyRequestQueue.getInstance(context);
		RequestQueue queue = VolleyRequestQueue.getRequestQueue();
		queue.add(xmlRequest);
		return newslistInstance;
	}
}
